package org.jeecg.modules.house.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlUnorderedList;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.house.PaHouse;
import org.jeecg.modules.house.entity.HouseSubBlock;
import org.jeecg.modules.house.entity.PaHouseProject;
import org.jeecg.modules.house.service.IHouseSubBlockService;
import org.jeecg.modules.house.service.IPaHouseProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

/**
 * @Description: TODO
 * @author: zhongyin
 * @date: 2021年03月03日 22:18
 */
@RestController
@RequestMapping("/house/task")
@Slf4j
public class HouseTask {
    @Autowired
    private IHouseSubBlockService houseSubBlockService;
    @Autowired
    private IPaHouseProjectService paHouseProjectService;
    private static class innerWebClient{
        private static final WebClient webClient = new WebClient();
    }
    /**
     * 爬取数据
     *
     * @return
     */
    @AutoLog(value = "爬取数据")
    @ApiOperation(value="爬取数据", notes="爬取数据")
    @GetMapping(value = "/start")
    public Result<?> paqu() throws IOException {
        // 获取指定网页实体
        HtmlPage page = getHtmlPage("https://newhouse.0557fdc.com/");

        List<HtmlElement> aList =(List<HtmlElement>)page.getByXPath("//li[@class='nav1']/a");
        for (HtmlElement aElement : aList) {
            System.out.println(aElement.asText());
            HtmlPage detailPage = aElement.click();
            List<HtmlUnorderedList> houseDetailList = (List<HtmlUnorderedList>) detailPage.getByXPath("//div[@class='Contentxt']/ul");
            for (HtmlUnorderedList htmlUnorderedList : houseDetailList) {
                System.out.println(htmlUnorderedList.asText());
            }
            /*List<HtmlDivision> detailList = (List<HtmlDivision>) detailPage.getByXPath("//div[@class='beihui']");
            for (HtmlDivision html : detailList) {
                System.out.println(html.asXml());
                List<HtmlElement> trList = html.getHtmlElementsByTagName("tr");
                for (HtmlElement element : trList) {
                    List<HtmlElement> tdList = element.getHtmlElementsByTagName("td");
                    for (HtmlElement tdElement : tdList) {
                        System.out.println(tdElement.asText());
                        String[] keyValue = tdElement.asText().split("：");
                        if(keyValue[0].equals("栋号")){
                            System.out.println(StrUtil.trim(keyValue[1]));
                        }
                        if(keyValue[0].equals("已备案")){
                            System.out.println(StrUtil.trim(keyValue[1].substring(0,keyValue[1].length()-1)));
                        }
                        if(keyValue[0].equals("许可证号码")){
                            System.out.println(StrUtil.trim(keyValue[1]));
                        }
                        if(keyValue[0].equals("已签约")){
                            System.out.println(StrUtil.trim(keyValue[1].substring(0,keyValue[1].length()-1)));
                        }
                        if(keyValue[0].equals("地上楼层")){
                            System.out.println(StrUtil.trim(keyValue[1]));
                        }
                        if(keyValue[0].equals("可售")){
                            System.out.println(StrUtil.trim((keyValue[1].substring(0,keyValue[1].length()-1))));
                        }
                        if(keyValue[0].equals("地下楼层")){
                            System.out.println(StrUtil.trim(keyValue[1]));
                        }
                        HtmlPage dongDetailPage = tdElement.click();
                        List<HtmlUnorderedList> dongList = (List<HtmlUnorderedList>) dongDetailPage.getByXPath("//div[@class='content_xiangx']/ul");
                        for (HtmlUnorderedList ulList : dongList) {
                            System.out.println(ulList.asText());
                            String[] keyValue2 = ulList.asText().split("：");
                            if(keyValue2[0].equals("套数")){
                                System.out.println(keyValue2[1].substring(0,keyValue2[1].length() - 1));
                            }
                            if(keyValue2[0].equals("规划许可证")){
                                System.out.println(StrUtil.trim(keyValue2[1]));
                            }
                            if(keyValue2[0].equals("建筑结构")){
                                System.out.println(StrUtil.trim(keyValue2[1]));
                            }
                            if(keyValue2[0].equals("预售许可面积")){
                                System.out.println(StrUtil.trim(keyValue2[1].substring(0,keyValue2[1].length() - 4)));
                            }
                            if(keyValue2[0].equals("网上销售总面积")){
                                System.out.println(StrUtil.trim(keyValue2[1].substring(0,keyValue2[1].length() - 4)));
                            }
                            if(keyValue2[0].equals("预售日期")){
                                System.out.println(StrUtil.trim(keyValue2[1]));
                            }

                        }
                    }
                }
            }*/
        }
        return Result.OK();
    }


    /**
     * 获取指定网页实体
     * @param url
     * @return
     */
    public static HtmlPage getHtmlPage(String url){
        //调用此方法时加载WebClient
        WebClient webClient = HouseTask.innerWebClient.webClient;
        // 取消 JS 支持
        webClient.setJavaScriptEnabled(false);
        // 取消 CSS 支持
        webClient.setCssEnabled(false);
        HtmlPage page=null;
        try{
            // 获取指定网页实体
            page = (HtmlPage) webClient.getPage(url);
        }catch (MalformedURLException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return page;
    }


    public static void main(String[] args) throws Exception {
        String a = "29359.16 平方米";
//        System.out.println(a.substring(0,-1));
//        System.out.println(a.substring(0,-2));
        System.out.println(a.substring(0,a.length()-4));
        // 获取指定网页实体
        /*HtmlPage page = getHtmlPage("https://newhouse.0557fdc.com/");

        List<HtmlElement> aList =(List<HtmlElement>)page.getByXPath("//li[@class='nav1']/a");
        for (HtmlElement aElement : aList) {
            System.out.println(aElement.asText());
            HtmlPage detailPage = aElement.click();
            List<HtmlElement> detailList = (List<HtmlElement>)detailPage.getByXPath("//div[@class='beihui']/a");
            for (HtmlElement htmlElement : detailList) {
                System.out.println(htmlElement.asXml());
            }
        }*/
        /*HtmlPage detailPage = getHtmlPage("https://newhouse.0557fdc.com/item/1321-2423-0088-2326446824529");
        List<HtmlDivision> detailList = (List<HtmlDivision>) detailPage.getByXPath("//div[@class='beihui']");
        for (HtmlDivision html : detailList) {
            System.out.println(html.asXml());
            List<HtmlElement> trList = html.getHtmlElementsByTagName("tr");
            for (HtmlElement element : trList) {
                List<HtmlElement> tdList = element.getHtmlElementsByTagName("td");
                for (HtmlElement tdElement : tdList) {
                    System.out.println(tdElement.asText());
                    String[] keyValue = tdElement.asText().split("：");
                    if(keyValue[0].equals("栋号")){
                        System.out.println(StrUtil.trim(keyValue[1]));
                    }
                    if(keyValue[0].equals("已备案")){
                        System.out.println(StrUtil.trim(keyValue[1].substring(0,keyValue[1].length()-1)));
                    }
                    if(keyValue[0].equals("许可证号码")){
                        System.out.println(StrUtil.trim(keyValue[1]));
                    }
                    if(keyValue[0].equals("已签约")){
                        System.out.println(StrUtil.trim(keyValue[1].substring(0,keyValue[1].length()-1)));
                    }
                    if(keyValue[0].equals("地上楼层")){
                        System.out.println(StrUtil.trim(keyValue[1]));
                    }
                    if(keyValue[0].equals("可售")){
                        System.out.println(StrUtil.trim((keyValue[1].substring(0,keyValue[1].length()-1)));
                    }
                    if(keyValue[0].equals("地下楼层")){
                        System.out.println(StrUtil.trim(keyValue[1]));
                    }
                    HtmlPage dongDetailPage = tdElement.click();
                    List<HtmlDivision> dongList = (List<HtmlDivision>) dongDetailPage.getByXPath("//div[@class='beihui']");
                }
            }
        }*/

        HtmlPage dongDetailPage =  getHtmlPage("https://newhouse.0557fdc.com/details/XegvPf4G5jgYKT7PfDMGINJKiWXefJ08wNc9ZDvROSNQUWwoXKNmK9eEYEAKSASTxjS4kn6zb75NnaedQOzL0pvnWEa4a9904rrbLNB3ZGmmQnUEIseY94B0frNDDVrh2snPjjr3xswX-NObHvufCRNC12JJ4s-MJVGSQrIkDh8=");
        List<HtmlUnorderedList> dongList = (List<HtmlUnorderedList>) dongDetailPage.getByXPath("//div[@class='content_xiangx']/ul");
        for (HtmlUnorderedList ulList : dongList) {
            System.out.println(ulList.asText());
            String[] keyValue = ulList.asText().split("：");
            if(keyValue[0].equals("套数")){
                System.out.println(keyValue[1].substring(0,keyValue[1].length() - 1));
            }
            if(keyValue[0].equals("规划许可证")){
                System.out.println(StrUtil.trim(keyValue[1]));
            }
            if(keyValue[0].equals("建筑结构")){
                System.out.println(StrUtil.trim(keyValue[1]));
            }
            if(keyValue[0].equals("预售许可面积")){
                System.out.println(StrUtil.trim(keyValue[1].substring(0,keyValue[1].length() - 4)));
            }
            if(keyValue[0].equals("网上销售总面积")){
                System.out.println(StrUtil.trim(keyValue[1].substring(0,keyValue[1].length() - 4)));
            }
            if(keyValue[0].equals("预售日期")){
                System.out.println(StrUtil.trim(keyValue[1]));
            }

        }

    }
}
